package com.yellowpage.bigdata.service;

import java.util.List;
import java.util.Map;

import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.opensymphony.xwork2.ActionContext;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;
import com.yellowpage.bigdata.data.NspFatallogImeiMonthregionSum;
import com.yellowpage.bigdata.dao.NspFatallogImeiMonthregionSumDao;
import com.yellowpage.ca.data.User;
import com.yellowpage.ca.service.RegionService;

public class NspFatallogImeiMonthregionSumServiceImpl extends BaseServiceImpl implements NspFatallogImeiMonthregionSumService {
	  private NspFatallogImeiMonthregionSumDao nspFatallogImeiMonthregionSumDao;
	  public void setNspFatallogImeiMonthregionSumDao(NspFatallogImeiMonthregionSumDao nspFatallogImeiMonthregionSumDao) {
		  this.nspFatallogImeiMonthregionSumDao = nspFatallogImeiMonthregionSumDao;
	  }
	
	
	  /* Smooth generate Method */ 
	  public NspFatallogImeiMonthregionSum getNspFatallogImeiMonthregionSum(Long id){
	      return nspFatallogImeiMonthregionSumDao.get(id);
	  }
	  
    public List<NspFatallogImeiMonthregionSum> getAllNspFatallogImeiMonthregionSum(){
        return nspFatallogImeiMonthregionSumDao.getAll();
    }
     
    public List<Map> queryNspFatallogImeiMonthregionSumByPage(Map queryMap, PageRender pageRender) {		
    
		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
				"SELECT a.FATALLOG_IMEI_MONTHREGION_SUM_ID ,(select c.COMPANY from NSP_REQUEST_TYPE c where c.CODE = a.REQUEST_TYPE)  as REQUEST_TYPE ,a.DATA_FLAG, a.IMEI_ALL_COUNT , a.IMEI_NEW_COUNT , a.IMEI_MONTH_COUNT ,a.REQUEST_COUNT,a.REQUEST_IMEI_COUNT,a.REQUEST_NoIMEI_COUNT, a.CREATE_USER , a.REGION_CODE ,(select c.name from OMC_REGION c where c.REGION_CODE=a.region_code) as REGION_NAME, a.CREATE_TIME , a.NOTE , a.LOG_MONTH    FROM NSP_FATALLOG_IMEI_MONTHREGION_SUM a WHERE 1=1 ")
				    			.addCondition(" and FATALLOG_IMEI_MONTHREGION_SUM_ID = :fatallogImeiMonthregionSumId ")
							    			.addCondition(" and REQUEST_TYPE = :requestType ")
							    			.addCondition(" and IMEI_ALL_COUNT = :imeiAllCount ")
							    			.addCondition(" and IMEI_NEW_COUNT = :imeiNewCount ")
							    			.addCondition(" and IMEI_MONTH_COUNT = :imeiMonthCount ")
							    			.addCondition(" and CREATE_USER = :createUser ")
							    			.addCondition(" and REGION_CODE like :regionCode ")
							    		    .addCondition(" and date_format(CREATE_TIME,'%Y-%m-%d') =  :createTime  ")
		    				    			.addCondition(" and NOTE = :note ")
							    			.addCondition(" and LOG_MONTH >= :query_logDate ")
							    			.addCondition(" and LOG_MONTH <= :query_logDate_end ")
							    ; 		    
		    

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}
    
    
    public List<Map> queryNspFatallogImeiMonthregionSumByCondition(Map queryMap, PageRender pageRender) {		
    	String t3 = queryMap.get("requestType").equals("") ? "" : " and REQUEST_TYPE in ( "+queryMap.get("requestType")+") ";
		String t4 = queryMap.get("regionCode").equals("") ? "" : " and REGION_CODE in ( "+queryMap.get("regionCode")+") ";
		String t1 = queryMap.get("query_logDate_end").equals("") ? " and LOG_MONTH <= (select max(log_date) from NSP_FATALLOG_REQUSET_SUM)" : " and LOG_MONTH <= '"+queryMap.get("query_logDate_end")+"'";
		String t2 = queryMap.get("logDate").equals("") ? " and LOG_MONTH >= (select min(log_date) from NSP_FATALLOG_REQUSET_SUM)" : " and LOG_MONTH >= '"+queryMap.get("logDate")+"'";
		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
    			"SELECT a.FATALLOG_IMEI_MONTHREGION_SUM_ID ,(select c.COMPANY from NSP_REQUEST_TYPE c where c.CODE = a.REQUEST_TYPE)  as REQUEST_TYPE ,(SELECT c.`NAME` from NSP_CHANNEL c ,NSP_REQUEST_TYPE n where c.`CODE` = n.CHANNEL_CODE and  n.`CODE`=a.REQUEST_TYPE)  as CHANNEL_NAME ,a.DATA_FLAG, a.IMEI_ALL_COUNT , a.IMEI_NEW_COUNT , a.IMEI_MONTH_COUNT ,a.REQUEST_COUNT,a.REQUEST_IMEI_COUNT,a.REQUEST_NoIMEI_COUNT, a.CREATE_USER , a.REGION_CODE ,(select c.name from OMC_REGION c where c.REGION_CODE=a.region_code) as REGION_NAME, a.CREATE_TIME , a.NOTE , a.LOG_MONTH    FROM NSP_FATALLOG_IMEI_MONTHREGION_SUM a WHERE 1=1"
    			+ t3+t4+t1+t2)
    			/*.addCondition(" and  "+t3)
    			.addCondition(" and  "+t4)*/
    			; 		    
    	
    	
    	List<Map> resultList = sqlAssistant.queryByPage(
    			baseDao.getConnection(), queryMap, pageRender);
    	
    	return resultList;
    }
    
    
    public void saveNspFatallogImeiMonthregionSum(NspFatallogImeiMonthregionSum nspFatallogImeiMonthregionSum){
        nspFatallogImeiMonthregionSumDao.save(nspFatallogImeiMonthregionSum);
    }
    
	  public void updateNspFatallogImeiMonthregionSum(NspFatallogImeiMonthregionSum nspFatallogImeiMonthregionSum){
	      nspFatallogImeiMonthregionSumDao.update(nspFatallogImeiMonthregionSum);
	  }
	  
	  public void delete(List deleteList){
	      nspFatallogImeiMonthregionSumDao.remove(deleteList);
	  }
	  
	  
	 
	
}	